package com.xe.icard;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.xe.framework.XEActivity;
import com.xe.framework.XEApp;
import com.xe.icard.R;
import com.xe.map.PlaceOverlay;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class Map extends XEActivity implements OnClickListener, MKSearchListener {

	private MapView mMapView = null;
	private MKSearch mSearch;
	private LocationClient mLocClient;
	private com.xe.icard.Map.NotifyLister mNotifyer;
	private MapController mapControl;
	private MyLocationOverlay myLocationOverlay;
	private LocationData locData;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        initMap(savedInstanceState);
        View back_btn = findViewById(R.id.back_btn);
        back_btn.setOnClickListener(this);
        View guide_btn = findViewById(R.id.guide_btn);
        guide_btn.setOnClickListener(this);
    }

    private void initMap(Bundle savedInstanceState) {
		//super.initMapActivity(XEApp.getInstance().getMapManager());

		mMapView = (MapView) findViewById(R.id.mapView);
		mMapView.setOnClickListener(this);
		// 设置在缩放动画过程中也显示overlay,默认为不绘制
		//mMapView.setDrawOverlayWhenZooming(true);
		mapControl = mMapView.getController();
		mapControl.enableClick(true);
		mapControl.setZoom(13);

		initLocClient();
		initOverlay();
		initSearch();	   
		initMapCenter();
	}

	private void initMapCenter() {
		GeoPoint lastPoint = null;
		if (null != mLocClient) {
			BDLocation cur = mLocClient.getLastKnownLocation();
			if (null != cur) {
				lastPoint = new GeoPoint((int) (cur.getLatitude() * 1E6),
						(int) (cur.getLongitude() * 1E6));
			}
		}
		if (null == lastPoint) {
			lastPoint = new GeoPoint((int) (Config.LATITUDE * 1E6),
					(int) (Config.LONGITUDE * 1E6)); // 默认设置结婚地点
		}
		mapControl.setCenter(lastPoint);
	}

	BDLocationListener myLisetener = new BDLocationListener(){

		public void onReceiveLocation(BDLocation location) {
			if(null != location){
				//mLocClient.stop();
				//当前坐标变化时，地图跟随定位点
				GeoPoint p = new GeoPoint((int) (location.getLatitude() * 1E6),
						(int) (location.getLongitude() * 1E6));
				locData.latitude = location.getLatitude();
		        locData.longitude = location.getLongitude();
		        locData.direction = 2.0f;
		        locData.accuracy = location.getRadius();
		        locData.direction = location.getDerect();
				myLocationOverlay.setData(locData);
				mapControl.animateTo(p);
				mMapView.refresh();
			}
		}

		public void onReceivePoi(BDLocation arg0) {
		}
		
	};
	
	private void initLocClient() {
        mLocClient = new LocationClient( this );
        mLocClient.registerLocationListener( myLisetener );
        
        //位置提醒相关代码
        mNotifyer = new NotifyLister();
	    mNotifyer.SetNotifyLocation(Config.LATITUDE, Config.LONGITUDE, 3000, "bd09ll");//4个参数代表要位置提醒的点的坐标，具体含义依次为：纬度，经度，距离范围，坐标系类型(gcj02,gps,bd09,bd09ll)
	    mLocClient.registerNotify(mNotifyer);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);//打开gps
        option.setCoorType("bd09ll");     //设置坐标类型
        option.setScanSpan(15000);
        mLocClient.setLocOption(option);
        mLocClient.start();
	}

    private void initSearch() {
		mSearch = new MKSearch();
		mSearch.init(XEApp.getInstance().getMapManager(), this);
	}
    
	private void initOverlay() {
		// 将自己加入地图覆盖层
		myLocationOverlay = new MyLocationOverlay(mMapView);
		locData = new LocationData();
	    myLocationOverlay.setData(locData);
		mMapView.getOverlays().add(myLocationOverlay);
		myLocationOverlay.enableCompass();
		
		// 将地址加入地图覆盖层
		Drawable drawble = getResources().getDrawable(R.drawable.invitation_place);
		ItemizedOverlay<OverlayItem> place = new PlaceOverlay(drawble, this);
		mMapView.getOverlays().add(place); // 添加ItemizedOverlay实例到mMapView
		mMapView.refresh();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_btn:
			XEApp.getInstance().getActivityManager().goBack(R.anim.in_from_left, R.anim.out_to_right);
			break;
		case R.id.guide_btn:
			MKPlanNode start = new MKPlanNode();
			BDLocation cur = mLocClient.getLastKnownLocation();
			mLocClient.start();
			LocationClientOption option = mLocClient.getLocOption();
			option.setScanSpan(60000);//60s
			if (null != cur) {
				
				GeoPoint lastPoint = new GeoPoint((int) (cur.getLatitude() * 1E6),
						(int) (cur.getLongitude() * 1E6));
				start.pt = lastPoint;

				MKPlanNode end = new MKPlanNode();
				end.pt = new GeoPoint((int)(Config.LATITUDE*1E6), (int)(Config.LONGITUDE*1E6));
				mSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
				mSearch.drivingSearch(null, start, null, end);
			}else{
				Toast.makeText(this, "亲，还未找到您的位置，请稍候重试", Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(KeyEvent.KEYCODE_BACK == keyCode){
			XEApp.getInstance().getActivityManager().goBack(R.anim.in_from_left, R.anim.out_to_right);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onDestroy() {
		mMapView.destroy();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	mMapView.onSaveInstanceState(outState);
    	
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	mMapView.onRestoreInstanceState(savedInstanceState);
    }

//	public void onReceiveLocation(BDLocation location) {
//		if(null != location){
//			mLocClient.stop();
//			//当前坐标变化时，地图跟随定位点
//			GeoPoint p = new GeoPoint((int) (location.getLatitude() * 1E6),
//					(int) (location.getLongitude() * 1E6));
//			locData.latitude = location.getLatitude();
//	        locData.longitude = location.getLongitude();
//	        locData.direction = 2.0f;
//	        locData.accuracy = location.getRadius();
//	        locData.direction = location.getDerect();
//			myLocationOverlay.setData(locData);
//			mapControl.animateTo(p);
//			mMapView.refresh();
//		}
//	}
//
//	public void onReceivePoi(BDLocation arg0) {
//	}

	public void onGetAddrResult(MKAddrInfo arg0, int arg1) {	
	}

	public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
	}

	public void onGetDrivingRouteResult(MKDrivingRouteResult result, int arg1) {
		if (result == null) {
	        return;
	    }
	    RouteOverlay routeOverlay = new RouteOverlay(Map.this, mMapView);
	    // 此处仅展示一个方案作为示例
	    routeOverlay.setData(result.getPlan(0).getRoute(0));
	    mMapView.getOverlays().add(routeOverlay);
	    mMapView.refresh();
	}

	public void onGetPoiDetailSearchResult(int arg0, int arg1) {
	}

	public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
	}

	public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {		
	}

	public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
			
	}

	public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
	}
	
	public class NotifyLister extends BDNotifyListener{
        public void onNotify(BDLocation mlocation, float distance) {
        	
        }
    }
}
